function slider(config){
	this.o = $(config.container);
	this.prevBtn = $(config.btns[0]);
	this.nextBtn = $(config.btns[1]);
	this.styles = config.btnStyle;
	this.iw = config.itemWidth || 0;
	this.vn = config.visibleNum || 1;
	this.showNum = 0;
	this.o.width(99999999);
	this.startPos = parseInt(this.o.css('left'));

	var _this = this;

	if(config.itemHoverStyle){
		this.o.on('mouseover', 'li', function(){
			$(this).addClass(config.itemHoverStyle);
		}).on('mouseout', 'li', function(){
			$(this).removeClass(config.itemHoverStyle);
		});
	}
	if(config.click){
		this.o.on('click', 'li', config.click);
	}
	this.init();
	this.prevBtn.on('click', function(){
		_this.prev();
	});
	this.nextBtn.on('click', function(){
		_this.next();
	});
}
slider.prototype = {
	init:function(type){
	},
	getLen:function(){
		return this.o.children().length;
	},
	prev:function(){
		if(!this.prevBtn.hasClass('prev-hover') || this.showNum == 0)return false;
		var w;

		if(this.showNum >= this.vn){
			w = this.iw*this.vn;
			this.showNum -= this.vn;
		}else{
			w = this.iw*this.showNum;
			this.showNum = 0;
		}
		if(!this.nextBtn.hasClass(this.styles[1]))this.nextBtn.addClass(this.styles[1]);
		this.o.animate({'left': '+='+w});
		if(this.showNum == 0)this.prevBtn.removeClass('prev-hover');
	},
	next:function(){
		if(!this.nextBtn.hasClass('next-hover'))return false;

		var m = this.getLen() - this.vn - this.showNum, w;

		if(m > this.vn){
			w = this.vn*this.iw;
			this.showNum += this.vn;
		}else if(m == 0){
			return;
		}else{
			w = m*this.iw;
			this.showNum += m;
		}

		if(!this.prevBtn.hasClass(this.styles[0]))this.prevBtn.addClass(this.styles[0]);
		this.o.animate({'left': '-='+w});
		if((this.getLen() - this.vn - this.showNum) == 0)this.nextBtn.removeClass('next-hover');
	},
	contentChange:function(eventType){
		var n = this.getLen();
		if(n <= this.vn){
			if(this.prevBtn.hasClass(this.styles[0]))this.prevBtn.removeClass(this.styles[0]);
			if(this.nextBtn.hasClass(this.styles[1]))this.nextBtn.removeClass(this.styles[1]);
			this.o.animate({left:this.startPos});
		}else{
			if(eventType == 'add'){
				this.showNum = n-this.vn;
				this.o.animate({left: -(this.showNum*this.iw)+this.startPos});
			}else if(!this.nextBtn.hasClass(this.styles[1])){
				this.nextBtn.addClass(this.styles[1]);
			}
			var left = parseInt(this.o.css('left'));
			if(!this.prevBtn.hasClass(this.styles[0]))this.prevBtn.addClass(this.styles[0]);
			if(left > this.startPos){
				if(this.prevBtn.hasClass(this.styles[0]))this.prevBtn.removeClass(this.styles[0]);
				this.o.animate({'left': this.startPos});
			}else if(left <= -(n*this.iw-(n-this.vn)*this.iw)){
				if(this.nextBtn.hasClass(this.styles[1]))this.nextBtn.removeClass(this.styles[1]);
			}
			if(n == this.showNum){
				this.showNum -= this.vn;
				this.o.animate({left:-(this.showNum*this.iw)+this.startPos});
			}
		}
	}
};


(function(){
	if(typeof window.TxtLimit === 'function')return;
	var getLen = function(a){
		var b = (a || '').match(/[^\x00-\x80]/g);
		return a.length + (b?b.length:0);
	};
	window.TxtLimit = function(target, btn, tips, maxLength){
		maxLength = maxLength || 140;
		btn = $(btn);
		tips = $(tips);
		$(target).keyup(function(){
			var len = getLen(this.value);
			if(len === 0 || len >= maxLength){
				btn.removeClass('on');
			}else{
				btn.addClass('on');
			}
			len = maxLength - len;
			if(len < 0){
				len = Math.abs(len);
				tips.addClass('obvious').html('已超出<b>'+len+'</b>字');
			}else{
				tips.removeClass('obvious').html('你还可以输入<b>'+len+'</b>字');
			}
		});
	};
})();

(function(){
	if(typeof window.AjaxLoad === 'function')return;

	window.AjaxLoad = (function(){
		return function(config){
			var data = [], index = 0, pageSize = config.pageSize || 10,
				pageNumSize = config.pageNumSize || 6, allPageNum = 0
				selected = {count:0};

			if(config.selected){
				$.each(config.selected, function(i, d){
					selected[d] = 1;
				})
			}

			this.selected = selected;
			this.container = config.container;

			$.ajax({
				url:config.url,
				success:function(d){
					var tmp = toJSON(d);
					$.each(tmp.data, function(i, d){
						var j = parseInt(i/pageSize);
						if(typeof data[j] === 'undefined')data[j] = [];
						data[j][i%pageSize] = d;
					});
					allPageNum = parseInt(tmp.count/pageSize);
					if(tmp.count%pageSize !== 0)allPageNum++;
					$(config.container + '_pageCount').html(allPageNum);
					loadData(0);
					loadPageNum(index);
				}
			});

			function toJSON(d){
				var tmp = {data:[], count:0};
				try{
					tmp = eval('('+d+')');
				}catch(e){}
				return tmp;
			}

			function loadData(pageNum){
				if(pageNum < 0 || pageNum >= allPageNum)return;
				if(typeof data[pageNum] === 'undefined'){
					$.ajax({
						url:config.url.replace('{0}', pageNum).replace('{1}', pageNum+1).replace('{2}', pageSize),
						success:function(d){
							var tmp = {data:[], count:0};
							try{
								tmp = eval('('+d+')');
							}catch(e){}
							data[pageNum] = tmp.data;
							loadData(pageNum);
						}
					});
					return;
				}
				var ul = [];
				$.each(data[pageNum], function(i){
					ul[i] = '<li data="'+this.id+'"'+(selected[this.id]?' class="seled"':'')+'><img src="'+this.img+'" alt="" /><span class="name">'+this.name+'</span><div class="links"><a href="javascript:void(0)" class="comment"><i></i>('+this.comment+')</a><a href="javascript:void(0)" class="detail">详情</a></div><i>已选择</i></li>';
				});
				$(config.container).html('<ul>'+ul.join('')+'</ul>');
				$(config.container + '_curPageNum').html(pageNum + 1);
			}
			function loadPageNum(i){
				var m = i*pageSize,
					n = m + pageSize,
					ret = [];

				if(n>allPageNum)n = allPageNum;
				if(m>n)return index--;
				ret.push('<a href="#" class="cur">'+(++m)+'</a>');
				loadData(m-1);
				for(;m<n;){
					ret.push('<a href="#">'+(++m)+'</a>');
				}
				$(config.pageContainer).html(ret.join(''));
			}

			$(config.page).on('click', 'a', function(){
				var o = $(this);
				o.parent().find('a').removeClass('cur');
				o.addClass('cur');
				loadData(parseInt(o.html()) - 1);
				return false;
			}).on('click', '.prev,.next', function(){
				switch(this.className){
					case 'prev':
						if(index == 0)break;
						loadPageNum(--index);
						break;
					case 'next':
						if(index+1 === allPageNum)break;
						loadPageNum(++index);
						break;
				};
				return false;
			});


			$(config.container).on('click', 'li', (config.click || function(){})).on('mouseover', 'li', function(){
				$(this).addClass('hover');
			}).on('mouseout', 'li', function(){
				$(this).removeClass('hover');
			});
		}
	})();


})();

window.onload = function(){
	new TxtLimit('#lTxtArea', '#lTxtArea_btn', '#lTxtAreaTips');

	var S = new slider({
		container: '#slider_1>ul',
		btns: ['#slider_1_prev', '#slider_1_next'],
		btnStyle: ['prev-hover', 'next-hover'],
		itemWidth: 172,
		itemHoverStyle: 'hover',
		visibleNum: 5,
		click: autoRemove
	});

	var A = new AjaxLoad({
		url: '/api.php?s={0}&e={1}',
		container: '#ID2',
		page: '#ID2_page',
		pageContainer: '#ID2_pageNum',
		pageSize: 10,
		pageNumSize: 6,
		selected:[8, 14],
		click: autoAdd
	});

	function autoAdd(){
		var o = $(this);
		if(!o.hasClass('seled')){
			A.selected[o.attr('data')] = 1;
			o.addClass('seled').clone().attr('class', '').click(autoRemove).appendTo(S.o);
			S.contentChange('add');
		}
		return false;
	}
	function autoRemove(){
		$(A.container + ' li[data='+$(this).attr('data')+']').removeClass('seled');
		$(this).remove();
		A.selected[$(this).attr('data')] = 0;
		S.contentChange('remove');
		return false;
	}

	//var data = [], index = 0, pageSize = 6, selected = {count:0}, allPageNum = 0;
/*
	$('#ID2').html('Loading ...');
	function loadPageNum(i){
		var m = i*pageSize,
			n = m + pageSize,
			ret = [];

		if(n>allPageNum)n = allPageNum;
		if(m>n)return index--;
		ret.push('<a href="#" class="cur">'+(++m)+'</a>');
		loadData(m-1);
		for(;m<n;){
			ret.push('<a href="#">'+(++m)+'</a>');
		}
		$('#ID2_pageNum').html(ret.join(''));
	}

	function loadData(pageNum){
		if(pageNum < 0 || pageNum >= allPageNum)return;
		if(typeof data[pageNum] === 'undefined'){
			$('#ID2').html('Loading ...');
			$.ajax({
				url:'/api.php?s='+pageNum+'&e='+(pageNum+1),
				success:function(d){
					var tmp = {data:[], count:0};
					try{
						tmp = eval('('+d+')');
					}catch(e){}
					data[pageNum] = tmp.data;
					loadData(pageNum);
				}
			});
			return;
		}
		var ul = [];
		$.each(data[pageNum], function(i){
			ul[i] = '<li data="'+this.id+'"'+(selected[this.id]?' class="seled"':'')+'><img src="'+this.img+'" alt="" /><span class="name">'+this.name+'</span><div class="links"><a href="javascript:void(0)" class="comment"><i></i>('+this.comment+')</a><a href="javascript:void(0)" class="detail">详情</a></div><i>已选择</i></li>';
		});
		$('#ID2').html('<ul>'+ul.join('')+'</ul>');
	}



	$('#ID2_page').on('click', 'a', function(){
		var o = $(this);
		o.parent().find('a').removeClass('cur');
		o.addClass('cur');
		loadData(parseInt(o.html()) - 1);
		return false;
	}).on('click', 'i', function(){
		switch(this.className){
			case 'prev':
				if(index == 0)break;
				loadPageNum(--index);
				break;
			case 'next':
				loadPageNum(++index);
				break;
		};
		return false;
	});


	$('#ID2').on('click', 'li', function(){
		var o = $(this);
		if(!o.hasClass('seled')){
			selected[o.attr('data')] = 1;
			o.addClass('seled').clone().attr('class', '').click(autoRemove).appendTo(s.o);
			s.contentChange('add');
		}
		return false;
	});

	$('#ID2').on('mouseover', 'li', function(){
		$(this).addClass('hover');
	}).on('mouseout', 'li', function(){
		$(this).removeClass('hover');
	});
	*/

}
